package com.cqdev.washer.api.controller;

import com.cqdev.washer.api.model.ScheduleJobVO;
import com.cqdev.washer.biz.model.BusinessParamBO;
import com.cqdev.washer.biz.model.ProjectBO;
import com.cqdev.washer.biz.service.BusinessService;
import com.cqdev.washer.biz.service.ProjectService;
import com.cqdev.washer.biz.service.ScheduleJobService;
import com.cqdev.washer.infra.util.JsonMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

/**
 * @author Yunfei.li
 */
@Controller
public class ScheduleJobController {


    private final ScheduleJobService scheduleJobService;
    private final ProjectService projectService;

    public ScheduleJobController(ScheduleJobService scheduleJobService, ProjectService projectService) {
        this.scheduleJobService = scheduleJobService;
        this.projectService = projectService;
    }

    /**
     * 新增任务页面
     */
    @RequestMapping(value = "input-schedule-job", method = RequestMethod.GET)
    public String inputScheduleJob(ScheduleJobVO scheduleJobVO, ModelMap modelMap) {
        if (scheduleJobVO.getId() != null) {
            ScheduleJobVO scheduleJob = scheduleJobService.get(scheduleJobVO.getId());

            BusinessParamBO businessParamBO = JsonMapper.nonEmptyMapper().fromJson(scheduleJob.getJobMethodParamValue(), BusinessParamBO.class);
            scheduleJob.setProjectId(businessParamBO.getProjectId());

            modelMap.put("scheduleJobVO", scheduleJob);
        }
        List<ProjectBO> projectBOList = projectService.listAll();
        modelMap.put("projects", projectBOList);
        return "input-schedule-job";
    }

    /**
     * 删除任务
     */
    @RequestMapping(value = "delete-schedule-job", method = RequestMethod.GET)
    public String deleteScheduleJob(Long scheduleJobId) {
        scheduleJobService.delete(scheduleJobId);
        return "redirect:list-schedule-job";
    }

    /**
     * 运行一次
     */
    @RequestMapping(value = "run-once-schedule-job", method = RequestMethod.GET)
    public String runOnceScheduleJob(Long scheduleJobId) {
        scheduleJobService.runOnce(scheduleJobId);
        return "redirect:list-schedule-job";
    }

    /**
     * 暂停
     */
    @RequestMapping(value = "pause-schedule-job", method = RequestMethod.GET)
    public String pauseScheduleJob(Long scheduleJobId) {
        scheduleJobService.pauseJob(scheduleJobId);
        return "redirect:list-schedule-job";
    }

    /**
     * 恢复
     */
    @RequestMapping(value = "resume-schedule-job", method = RequestMethod.GET)
    public String resumeScheduleJob(Long scheduleJobId) {
        scheduleJobService.resumeJob(scheduleJobId);
        return "redirect:list-schedule-job";
    }

    /**
     * 保存任务
     */
    @RequestMapping(value = "save-schedule-job", method = RequestMethod.POST)
    public String saveScheduleJob(ScheduleJobVO scheduleJobVO) {
        scheduleJobVO.setGmtCreate(new Date());
        scheduleJobVO.setJobClassOrInterfaceFullName(BusinessService.class.getName());
        scheduleJobVO.setJobMethodName("fetchOffer");
        scheduleJobVO.setJobMethodParamType(BusinessParamBO.class.getName());
        scheduleJobVO.setJobMethodParamValue("{\"projectId\":" + scheduleJobVO.getProjectId() + "}");
        //测试用随便设个状态
        scheduleJobVO.setStatus("1");
        if (scheduleJobVO.getId() == null) {
            scheduleJobService.insert(scheduleJobVO);
        } else {
            scheduleJobService.update(scheduleJobVO);
        }
        return "redirect:list-schedule-job";
    }

    /**
     * 任务列表页
     */
    @RequestMapping(value = "list-schedule-job", method = RequestMethod.GET)
    public String listScheduleJob(ScheduleJobVO scheduleJobVO, ModelMap modelMap) {

        List<ScheduleJobVO> scheduleJobVOList = scheduleJobService.queryList(scheduleJobVO);
        modelMap.put("scheduleJobVOList", scheduleJobVOList);

        List<ScheduleJobVO> executingJobList = scheduleJobService.queryExecutingJobList();
         modelMap.put("executingJobList", executingJobList);

        return "list-schedule-job";
    }

}
